package qeug.shsh.opplugin;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.AppInfo;
import kd.bos.entity.AppMetadataCache;
import kd.bos.entity.ExtendedDataEntity;
import kd.bos.entity.MainEntityType;
import kd.bos.entity.flex.FlexEntireData;
import kd.bos.entity.flex.FlexEntityMetaUtils;
import kd.bos.entity.flex.FlexProperty;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.entity.param.AppCustomParam;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.AddValidatorsEventArgs;
import kd.bos.entity.plugin.args.AfterOperationArgs;
import kd.bos.entity.property.FlexProp;
import kd.bos.entity.validate.AbstractValidator;
import kd.bos.flex.FlexService;
import kd.bos.orm.ORM;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import kd.bos.servicehelper.parameter.SystemParamServiceHelper;
import okhttp3.*;
import qeug.shsh.bpm.Factory.ThirdServiceFactory;
import qeug.shsh.bpm.service.SAPService;
import qeug.shsh.bpm.service.impl.SAPServicelmpl;
import qeug.shsh.helper.FacadeHelper;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @projectName: qeug-cosmic-root-project
 * @package: qeug.shsh.opplugin
 * @className: VoucherBillSAPPlugin
 * @author: xuhaihui
 * @description: 凭证推送SAP按钮插件
 * @date: 2024/3/13
 * @version: 1.0
 */

public class VoucherBillSAPPlugin extends AbstractOperationServicePlugIn {

    @Override
    public void afterExecuteOperationTransaction(AfterOperationArgs e) {
        super.afterExecuteOperationTransaction(e);
        String operationKey = e.getOperationKey();

        //根据应用编码从缓存中获取应用信息
        AppInfo cgfwAppInfo = AppMetadataCache.getAppInfo("gl");
        //获取应用的主键
        String appId = cgfwAppInfo.getId();
        AppCustomParam appCustomParam = new AppCustomParam(appId);

//        appCustomParam.getSearchKeySet().add("cs");

        Map<String, String> cusTomMap = SystemParamServiceHelper.loadAppCustomParameterFromCache(appCustomParam);
        System.out.println();
//        try {
//            List<ExtendedDataEntity> selectedRows = e.getSelectedRows();
//            for (ExtendedDataEntity selectedRow : selectedRows) {
//                Object id = selectedRow.getValue("id");
//                QFilter[] qFilters = new QFilter[]{new QFilter("id", "=", id)};
//
//                DynamicObject gl_voucher = BusinessDataServiceHelper.load呃呃呃呃呃呃呃呃呃呃呃、Single("gl_voucher", qFilters);
//                DynamicObject org = gl_voucher.getDynamicObject("org");//核算组织
//                String orgId = org.getString("id");
//                long orgIdLong = Long.parseLong(orgId);
//                QFilter[] qFilter = new QFilter[]{
//                        new QFilter("id", QCP.equals, orgIdLong)
//                };
//                DynamicObject bos_org = BusinessDataServiceHelper.loadSingle("bos_org", "qeug_issap", qFilter);
//                boolean isSap = bos_org.getBoolean("qeug_issap");//是否推送SAP标识
//                //判断组织中字段“是否推送SAP”为是还是否，为是才能推送SAP
//                if (isSap) {
//
//                } else {
//
//                    throw new IllegalArgumentException("当前核算组织不能推送SAP");
//                }
//
//                if ("donothing".equals(operationKey)) {
//                    SAPService sapService = ThirdServiceFactory.createSAPService();
//                    String voucher = sapService.voucher(gl_voucher);
//                } else if ("".equals(operationKey)) {
////                    String backVoucher = sapService.backVoucher(gl_voucher);
//                }
//            }
//        } catch (Exception ex) {
//            throw new RuntimeException(ex);
//        }
    }

    @Override
    public void onAddValidators(AddValidatorsEventArgs e) {
        super.onAddValidators(e);
        List<AbstractValidator> validators = e.getValidators();
        // 删除标品校验器
//        validators.removeIf(validator -> validator instanceof PaymentMaxAmountValidator);
//        validators.removeIf(validator -> validator instanceof PaymentTypeValidator);
        // 添加定制二开的校验器（位置放第一位，优先执行）
        validators.add(0, new VoucherSAPValidator());
    }
}
